package com.nfIt.dao.Impl;

import com.nfIt.dao.staffsDao;
import com.nfIt.jdbcDbutils.Dbutlis;
import com.nfIt.pojo.staffs;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * ClossName:staffsDaoImpl
 * Pakage:com.nfIt.Dao
 * Description
 *
 * @Author L
 * @Create 2024/3/6 20:20
 * @Version 2023.1.3
 */
public class staffsDaoImpl implements staffsDao {

    @Override
    public int insertStaffs(staffs s) {
        int rows = 0 ;

        Connection conn=null;
        PreparedStatement pstm=null;

        try {
            conn = Dbutlis.getConnection();

            String sql = "insert into staffs(name,age,phone,pos,add_time,update_time) values(?,?,?,?,?,?)";

             pstm = conn.prepareStatement(sql);

            pstm.setString(1,s.getName());
            pstm.setInt(2,s.getAge());
            pstm.setString(3,s.getPhone());
            pstm.setString(4,s.getPos());
            pstm.setDate(5, (Date) s.getAdd_time());
            pstm.setDate(6, (Date) s.getUpdate_time());

            rows = pstm.executeUpdate();
            System.out.println(rows);


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            Dbutlis.close(pstm,conn);
        }

        return rows;
    }

    @Override
    public List<staffs> staffsAll() {

        ArrayList<staffs> list = new ArrayList<>();

        String sql = "select * from staffs";

        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
             conn = Dbutlis.getConnection();
             pstm =conn.prepareStatement(sql);
             rs = pstm.executeQuery();
             while (rs.next()){
                staffs s = new staffs();
                s.setId(rs.getInt(1));
                s.setName(rs.getString(2));
                s.setAge(rs.getInt(3));
                s.setPhone(rs.getString(4));
                s.setPos(rs.getString(5));
                s.setAdd_time(rs.getDate(6));
                s.setUpdate_time(rs.getDate(7));

                list.add(s);
             }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            Dbutlis.close(rs,pstm,conn);
        }

        return list;
    }

    @Override
    public staffs staffsSeleId(int id) {

        String sql = "select * from staffs where id=?";
        Connection conn=null;
        PreparedStatement pstm =null;

        ResultSet rs =null;
        staffs s=null;
        try {
             conn = Dbutlis.getConnection();
             pstm =conn.prepareStatement(sql);

             pstm.setInt(1,id);

             rs = pstm.executeQuery();

             while (rs.next()){
                 s = new staffs();
                 s.setId(rs.getInt(1));
                 s.setName(rs.getString(2));
                 s.setAge(rs.getInt(3));
                 s.setPhone(rs.getString(4));
                 s.setPos(rs.getString(5));
                 s.setAdd_time(rs.getDate(6));
                 s.setUpdate_time(rs.getDate(7));
             }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return s;
    }

    @Override
    public List<staffs> staffsSelePag() {

        ArrayList<staffs> list = new ArrayList<>();

        String sql = "select * from staffs limit 5";
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs = null;
        try {
             conn = Dbutlis.getConnection();
             pstm = conn.prepareStatement(sql);
             rs =pstm.executeQuery();

             while (rs.next()){
                 staffs s = new staffs();
                 s.setId(rs.getInt(1));
                 s.setName(rs.getString(2));
                 s.setAge(rs.getInt(3));
                 s.setPhone(rs.getString(4));
                 s.setPos(rs.getString(5));
                 s.setAdd_time(rs.getDate(6));
                 s.setUpdate_time(rs.getDate(7));

                 list.add(s);
             }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            Dbutlis.close(rs,pstm,conn);
        }

        return list;
    }

    @Override
    public int staffsDeleId(int id) {

        int rows = 0;
        String sql = "delete from staffs where id=?";
        Connection conn =null;
        PreparedStatement pstm =null;
        try {
             conn = Dbutlis.getConnection();
             pstm = conn.prepareStatement(sql);

             pstm.setInt(1,id);

             rows = pstm.executeUpdate();


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            Dbutlis.close(pstm,conn);
        }
        return rows;
    }

    @Override
    public int staffsUpdaId(staffs s) {

        int rows = 0;
        String sql = "update staffs set name=?,age=?,pos=? where id=?";
        Connection conn=null;
        PreparedStatement pstm =null;

        try {
             conn = Dbutlis.getConnection();
             pstm =conn.prepareStatement(sql);

             pstm.setString(1,s.getName());
             pstm.setInt(2,s.getAge());
             pstm.setString(3,s.getPos());
             pstm.setInt(4,s.getId());

             rows = pstm.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            Dbutlis.close(pstm,conn);
        }
        return rows;
    }
}
